package com.ellecity06.common.base

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.viewbinding.ViewBinding
import com.ellecity06.common.R
import com.ellecity06.common.base.view.LoadUiIntent
import com.ellecity06.common.databinding.ActivityBaseBinding
import com.ellecity06.common.helper.LoginHelper
import com.ellecity06.common.utils.ToastUtils
import com.gyf.immersionbar.ktx.immersionBar
import java.lang.reflect.ParameterizedType

/**
 * @author ellecity06
 * @time 2023/3/29 15:06
 * @des mvvm版的基类
 */
abstract class BaseMvvmActivity<out VB : ViewBinding, out VM : BaseViewModel> : BaseActivity() {

    protected val viewModel: VM by lazy { initViewModel() }
    protected val binding: VB by lazy { inflateBinding() }
    private val baseBinding: ActivityBaseBinding by lazy { ActivityBaseBinding.inflate(layoutInflater) }
    abstract fun inflateBinding(): VB

    /**
     * 初始话 --onCrete
     */
    abstract fun initialize(savedInstanceState: Bundle?)

    /**
     * 配置项是否使用base的title
     */
    open val userBaseTitle = true

    /**
     * 初始化数据，网络请求数据初始化需要复写这个函数
     */

    open fun initData() = Unit
    open fun initObserver() = Unit
    open fun initEvent() = Unit
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (userBaseTitle) {
            baseBinding.layoutContent.addView(binding.root)
            setContentView(baseBinding.root)
        } else {
            setContentView(binding.root)
        }

        initBaseObserver()
        initBaseEvent()
        initObserver()
        initEvent()
        initialize(savedInstanceState)
        lifecycleScope.launchWhenCreated {
            initData()
        }


    }

    private fun initBaseEvent() {
        if (userBaseTitle) {
            baseBinding.ivBack.setOnClickListener {
                onBackPressed()
            }
        }
    }

    override fun initImmersionBar() {
        immersionBar {
            transparentBar()
            statusBarDarkFont(darkFont)
            navigationBarColor(R.color.transparent)
            navigationBarEnable(false)
            navigationBarWithKitkatEnable(false)
            if (userBaseTitle) titleBar(baseBinding.toolBar)
        }
    }


    /**
     * token过期
     */
    open fun onAuthorizationExpired(message: String) {
        ToastUtils.show(message)
        LoginHelper.instance.logout(this)
    }

    private fun initBaseObserver() {
        lifecycleScope.launchWhenStarted {
            viewModel.loadUiIntentFlow.collect {
                when (it) {
                    is LoadUiIntent.Error -> {
                        ToastUtils.show(it.msg)
                    }
                    is LoadUiIntent.Loading -> {
                        if (it.isShow) {
                            showLoading(text = it.text)
                        } else {
                            dismissLoading()
                        }
                    }

                    is LoadUiIntent.AuthorizationExpired -> {
                        onAuthorizationExpired(it.message)
                    }
                }
            }
        }
    }


    open fun initViewModel(): VM {
        val type = javaClass.genericSuperclass
        return if (type is ParameterizedType) {
            val tp = type.actualTypeArguments[1]
            val tClass = tp as? Class<VM> ?: BaseViewModel::class.java
            ViewModelProvider(this)[tClass] as VM
        } else {
            viewModels<BaseViewModel>().value as VM

        }

    }

    /**
     * 隐藏返回按钮
     */
    protected fun hideBackIcon() {
        if (userBaseTitle) baseBinding.ivBack.visibility = View.GONE
    }

    override fun setTitle(title: CharSequence?) {
        if (userBaseTitle) baseBinding.tvTitle.text = title
    }

    /**
     * 隐藏导航栏
     */
    protected fun hideToolBar() {
        baseBinding.toolBar.visibility = View.GONE
    }

    /**
     * 隐藏导航栏
     */
    protected fun showRightText() {
        baseBinding.tvTitleRight.visibility = View.VISIBLE
    }

    /**
     * 隐藏导航栏
     */
    protected fun hideRightText() {
        baseBinding.tvTitleRight.visibility = View.GONE
    }

    /**
     * 隐藏导航栏
     */
    protected fun setRightText(text: String) {
        showRightText()
        baseBinding.tvTitleRight.text = text
    }

    protected fun setRightClickListener(listener: View.OnClickListener) {
        baseBinding.tvTitleRight.setOnClickListener(listener)
    }
}